package com.jiu345.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.jiu345.api.pojo.vo.CategoryVo;
import com.jiu345.utils.GoodsCateNodeUtil;
import com.jiu345.api.mapper.GoodsCateMapper;
import com.jiu345.api.mapper.GoodsMapper;
import com.jiu345.api.pojo.Goods;
import com.jiu345.api.pojo.GoodsCategory;
import com.jiu345.api.service.IGoodsCateService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * package:IGoodsCateServiceImpl
 * @author MrLei QQ:235921049
 * @date 2024-05-21
 * desc:商品分类
 */
@RequiredArgsConstructor
@Service
public class IGoodsCateServiceImpl implements IGoodsCateService {
    private final GoodsCateMapper baseMapper;
    private final GoodsMapper goodsMapper;
//    private final RedisTemplate<String,String> redisTemplate;

    /*商品分类*/
    @Override
    public List<GoodsCategory> queryList(GoodsCategory bo) {

        LambdaQueryWrapper<GoodsCategory> wrapper = new LambdaQueryWrapper<>(bo);
        //查询所有分类列表
        List<GoodsCategory> catelist = baseMapper.selectList(wrapper);
        //TODO 这里不用递归方法
        //1、查询第一层
        List<GoodsCategory> oneList = catelist.stream().filter(x -> x.getParentId()==0).toList();
        //2、查询第二层
        oneList.forEach(one->{
            List<GoodsCategory> twoList = catelist.stream()
                .filter(x -> Objects.equals(x.getParentId(), one.getId())).toList();
            //封装
            one.setChildren(twoList);
            //查询第三层
            twoList.forEach(two->{
                List<GoodsCategory> threeList = catelist.stream()
                    .filter(x -> Objects.equals(x.getParentId(), two.getId())).toList();
                //封装
                two.setChildren(threeList);
            });
        });

        //查询分类下面的关联商品,这个功能是给首页banner左侧的导航栏商品使用，实际意义不大
//        catelist.forEach(x -> {
//            LambdaQueryWrapper<Goods> goodsWrapper = new LambdaQueryWrapper<>();
//            //商品ID=分类ID
//            goodsWrapper.eq(Goods::getCateId, x.getId());
//            List<Goods> frontGoods = goodsMapper.selectList();
//            x.setGoodsList(frontGoods);
//        });

        //用层级封装组件重新封装后返回
//        return GoodsCateNodeUtil.buildTree(catelist);
        return oneList;
    }

    /*分类详情*/
    @Override
    public GoodsCategory queryById(Long id) {
        //获取所有信息
        GoodsCategory category = baseMapper.selectById(id);
        if(category ==null){
            //如果找不到分类，返回null或抛出异常
            return null;
        }
        // 查询子分类
        LambdaQueryWrapper<GoodsCategory> childrenWrapper = new LambdaQueryWrapper<>();
        childrenWrapper.eq(GoodsCategory::getParentId, id);
        List<GoodsCategory> children = baseMapper.selectList(childrenWrapper);
        category.setChildren(children);
        //查询第三级的分类（也是最后一级的分类）
        //如果商品的cateID==三级分类的id，则开始查询其所有商品

        // 查询下属商品，下面没写对
//        LambdaQueryWrapper<Goods> goodsWrapper = new LambdaQueryWrapper<>();
        //这里应该查询对应的3级分类才有商品
//        goodsWrapper.eq(Goods::getCateId, id);
//        goodsWrapper.orderByDesc(Goods::getCreateTime);
//        List<Goods> goods = goodsMapper.selectList(goodsWrapper); // 使用goodsWrapper
//        category.setGoodsList(goods);

        return category;
    }

    /*二级分类详情*/
    @Override
    public GoodsCategory queryByTwoId(Long id) {
        //根据ID查询出这条数据的详情
        GoodsCategory cateTwo = baseMapper.selectById(id);
        //如果parentId大于0，则查询父级信息
        if (cateTwo != null && cateTwo.getParentId() > 0) {
            //只查询父级分类
            GoodsCategory parentCategory = baseMapper.selectById(cateTwo.getParentId());
            //如果父级分类存在，则设置到cateTwo中
            if (parentCategory != null) {
                cateTwo.setPId(parentCategory.getId());
                cateTwo.setPName(parentCategory.getName());
            }
        }
        return cateTwo;
    }
    /*获取所有一级分类*/
    @Override
    public List<GoodsCategory> getOne() {
        LambdaQueryWrapper<GoodsCategory> wrapper = new LambdaQueryWrapper<>();
        //查询pid=0的值，则是一级分类
        wrapper.eq(GoodsCategory::getParentId,0);
        return baseMapper.selectList(wrapper);
    }
    /*查询父级ID*/
    @Override
    public CategoryVo selectParentId(Long id) {
        LambdaQueryWrapper<GoodsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsCategory::getId,id);
        //3级详情
        GoodsCategory category3 = baseMapper.selectById(id);
        //2级详情
        GoodsCategory category2 = baseMapper.selectById(category3.getParentId());
        //1级详情
        GoodsCategory category1 = baseMapper.selectById(category2.getParentId());
        //返回数据
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setOneId(category1.getId());
        categoryVo.setTwoId(category2.getId());
        categoryVo.setThreeId(category3.getId());

        return categoryVo;
    }
    /*根据一级分类查询二级三级*/
    @Override
    public List<GoodsCategory> selectTwoList(Long id) {
        LambdaQueryWrapper<GoodsCategory> lqw = new LambdaQueryWrapper<>();
        //查询所有分类
        List<GoodsCategory> catelist = baseMapper.selectList(lqw);
        lqw.eq(GoodsCategory::getParentId,id);
        //查询前端传过来的所属的二级列表
        List<GoodsCategory> twoList = baseMapper.selectList(lqw);
        //2、查询第二层
        twoList.forEach(one->{
            List<GoodsCategory> threeList = catelist.stream()
                .filter(x -> Objects.equals(x.getParentId(), one.getId())).toList();
            //封装
            one.setChildren(threeList);
        });
        return twoList;
    }

}
